Completed
Push — master ( cb13e2...855cef )
by Yannick
34:31
created

map.common.js ➔ clickDisplayAirports   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 1
1
/**
2
 * This javascript is part of FlightAirmap.
3
 *
4
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
5
 * Licensed under AGPL license.
6
 * For more information see: https://www.flightairmap.com/
7
*/
8
"use strict";
9
function getCookie(cname) {
10
    var name = cname + "=";
11
    var ca = document.cookie.split(';');
12
    for(var i=0; i<ca.length; i++) {
13
	var c = ca[i];
14
	while (c.charAt(0)==' ') c = c.substring(1);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
15
	if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
16
    }
17
    return "";
18
}
19
20
function delCookie(cname) {
21
    document.cookie = cname + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT; path=/';
22
}
23
24
function createCookie(name, value, days) {
25
    var date, expires;
26
    if (days) {
27
	date = new Date();
28
	date.setTime(date.getTime()+(days*24*60*60*1000));
29
	expires = "; expires="+date.toGMTString();
30
    } else {
31
	expires = "";
32
    }
33
    document.cookie = name+"="+value+expires+"; path=/";
34
}
35
36
function dynamicSort(property) {
37
    var sortOrder = 1;
38
    if(property[0] === "-") {
39
        sortOrder = -1;
40
        property = property.substr(1);
41
    }
42
    return function (a,b) {
43
        var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
44
        return result * sortOrder;
45
    }
46
}
47
48
function dynamicSortMultiple() {
49
    var props = arguments;
50
    return function (obj1, obj2) {
51
	var i = 0, result = 0, numberOfProperties = props.length;
52
	while(result === 0 && i < numberOfProperties) {
53
	    result = dynamicSort(props[i])(obj1, obj2);
54
	    i++;
55
	}
56
	return result;
57
    }
58
}
59
60
function map2dlimit(limit) {
61
    createCookie('map_2d_limit',limit,9999);
62
}
63
64
function mapType(selectObj) {
65
    var idx = selectObj.selectedIndex;
66
    var atype = selectObj.options[idx].value;
67
    var type = atype.split('-');
68
    if (type[0] == 'Mapbox') {
69
	createCookie('MapType',type[0],9999);
70
	createCookie('MapTypeId',type[1],9999);
71
	if (getCookie('Map2D3DSync')) {
72
	    createCookie('MapType3D',type[0],9999);
73
	    createCookie('MapType3DId',type[1],9999);
74
	}
75
    } else {
76
	createCookie('MapType',atype,9999);
77
	if (getCookie('Map2D3DSync')) {
78
	    createCookie('MapType3D',atype,9999);
79
	}
80
    }
81
    window.location.reload();
82
}
83
function mapType3D(selectObj) {
84
    var idx = selectObj.selectedIndex;
85
    var atype = selectObj.options[idx].value;
86
    var type = atype.split('-');
87
    if (type[0] == 'Mapbox') {
88
	createCookie('MapType3D',type[0],9999);
89
	createCookie('MapType3DId',type[1],9999);
90
	if (getCookie('Map2D3DSync')) {
91
	    createCookie('MapType',type[0],9999);
92
	    createCookie('MapTypeId',type[1],9999);
93
	}
94
    } else {
95
	createCookie('MapType3D',atype,9999);
96
	if (getCookie('Map2D3DSync')) {
97
	    createCookie('MapType',atype,9999);
98
	}
99
    }
100
    window.location.reload();
101
}
102
function clickSyncMap2D3D(cb) {
103
    createCookie('Map2D3DSync',cb.checked,9999);
104
    if (cb.checked) {
105
	createCookie('MapType3D',getCookie('MapType'),9999);
106
	createCookie('MapType3DId',getCookie('MapTypeId'),9999);
107
    }
108
}
109
110
function terrainType(selectObj) {
111
    var idx = selectObj.selectedIndex;
112
    var atype = selectObj.options[idx].value;
113
    var type = atype.split('-');
114
    document.cookie =  'MapTerrain='+type+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/'
115
    createCookie('MapTerrain',type,9999);
116
    if (type == 'stk') {
117
	stkterrain();
118
    } else if (type == 'articdem') {
119
	articterrain();
120
    } else if (type == 'ellipsoid') {
121
	ellipsoidterrain();
122
    } else if (type == 'vrterrain') {
123
	vrtheworldterrain();
124
    }
125
    //window.location.reload();
126
}
127
128
function sattypes(selectObj) {
129
    var sattypes = [], sattype;
130
    for (var i=0, len=selectObj.options.length; i< len;i++) {
131
	sattype = selectObj.options[i];
132
	if (sattype.selected) {
133
	    sattypes.push(sattype.value);
134
	}
135
    }
136
    createCookie('sattypes',sattypes.join(),2);
137
    updateSat();
138
}
139
function airlines(selectObj) {
140
    var airs = [], air;
141
    for (var i=0, len=selectObj.options.length; i< len;i++) {
142
	air = selectObj.options[i];
143
	if (air.selected) {
144
	    airs.push(air.value);
145
	}
146
    }
147
    createCookie('filter_Airlines',airs.join(),2);
148
}
149
function airlinestype(selectObj) {
150
    var idx = selectObj.selectedIndex;
151
    var airtype = selectObj.options[idx].value;
152
    createCookie('filter_airlinestype',airtype,2);
153
}
154
function racefilter(selectObj) {
155
    var idx = selectObj.selectedIndex;
156
    var race = selectObj.options[idx].value;
157
    if (race == 'all') {
158
	delCookie('filter_race');
159
    } else {
160
	createCookie('filter_race',race,2);
161
    }
162
    if (getCookie['MapFormat'] == '3d') {
163
	updateMarineData();
164
    } else {
165
	getLiveMarineData(0);
166
    }
167
}
168
function alliance(selectObj) {
169
    var idx = selectObj.selectedIndex;
170
    var alliance = selectObj.options[idx].value;
171
    createCookie('filter_alliance',alliance,2);
172
}
173
function identfilter() {
174
    var ident = $("#identfilter").value;
175
    createCookie('filter_ident',ident,2);
176
}
177
function mmsifilter() {
178
    var ident = $("#mmsifilter").value;
179
    createCookie('filter_mmsi',ident,2);
180
}
181
function removefilters() {
182
    // Get an array of all cookie names (the regex matches what we don't want)
183
    var cookieNames = document.cookie.split(/=[^;]*(?:;\s*|$)/);
184
    // Remove any that match the pattern
185
    for (var i = 0; i < cookieNames.length; i++) {
186
	if (/^filter_/.test(cookieNames[i])) {
187
	    delCookie(cookieNames[i]);
188
	}
189
    }
190
    window.location.reload();
191
}
192
function sources(selectObj) {
193
    var sources = [], source;
194
    for (var i=0, len=selectObj.options.length; i< len;i++) {
195
	source = selectObj.options[i];
196
	if (source.selected) {
197
	    sources.push(source.value);
198
	}
199
    }
200
    createCookie('filter_Sources',sources.join(),2);
201
}
202
203
204
function show2D() {
205
    createCookie('MapFormat','2d',10);
206
    if (document.getElementById("pointtype").className == 'tracker') {
207
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
208
    } else if (document.getElementById("pointtype").className == 'marine') {
209
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
210
    } else {
211
	createCookie('MapTrack',document.getElementById("pointident").className,1);
212
    }
213
    window.location.reload();
214
}
215
function show3D() {
216
    createCookie('MapFormat','3d',10);
217
    if (document.getElementById("pointtype").className == 'tracker') {
218
	createCookie('MapTrackTracker',document.getElementById("pointident").className,1);
219
    } else if (document.getElementById("pointtype").className == 'marine') {
220
	createCookie('MapTrackMarine',document.getElementById("pointident").className,1);
221
    } else {
222
	createCookie('MapTrack',document.getElementById("pointident").className,1);
223
    }
224
    window.location.reload();
225
}
226
function clickPolar(cb) {
227
    createCookie('polar',cb.checked,9999);
228
    window.location.reload();
229
}
230
function clickDisplayAirports(cb) {
231
    createCookie('displayairports',cb.checked,9999);
232
    window.location.reload();
233
}
234
function clickDisplayISS(cb) {
235
    createCookie('displayiss',cb.checked,9999);
236
    updateSat();
237
}
238
function clickDisplayMinimap(cb) {
239
    createCookie('displayminimap',cb.checked,9999);
240
    if (cb.checked == true) {
241
	displayMiniMap();
242
    } else {
243
	window.location.reload();
244
    }
245
}
246
function clickShadows(cb) {
247
    createCookie('map3dnoshadows',cb.checked,9999);
248
    viewer.shadows = cb.checked;
0 ignored issues
show
Bug introduced by
The variable viewer seems to be never declared. If this is a global, consider adding a /** global: viewer */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
249
    //window.location.reload();
250
}
251
function clickSingleModel(cb) {
252
    createCookie('singlemodel',cb.checked,9999);
253
}
254
function clickUpdateRealtime(cb) {
255
    createCookie('updaterealtime',cb.checked,9999);
256
}
257
function clickVATSIM(cb) {
258
    createCookie('filter_ShowVATSIM',cb.checked,2);
259
}
260
function clickIVAO(cb) {
261
     createCookie('filter_ShowIVAO',cb.checked,2);
262
}
263
function clickphpVMS(cb) {
264
    createCookie('filter_ShowVMS',cb.checked,2);
265
}
266
function clickSBS1(cb) {
267
    createCookie('filter_ShowSBS1',cb.checked,2);
268
}
269
function clickBlocked(cb) {
270
    createCookie('filter_blocked',cb.checked,2);
271
}
272
function clickAPRS(cb) {
273
    createCookie('filter_ShowAPRS',cb.checked,2);
274
}
275
function clickDisplayGroundStation(cb) {
276
    createCookie('show_GroundStation',cb.checked,2);
277
    window.location.reload();
278
}
279
function clickDisplayWeatherStation(cb) {
280
    createCookie('show_WeatherStation',cb.checked,2);
281
    window.location.reload();
282
}
283
/*
284
function clickDisplayWeather(cb) {
285
    createCookie('show_Weather',cb.checked,2);
286
//    window.location.reload();
287
}
288
*/
289
function clickDisplayLightning(cb) {
290
    createCookie('show_Lightning',cb.checked,2);
291
    window.location.reload();
292
}
293
function clickDisplayFires(cb) {
294
    createCookie('show_Fires',cb.checked,2);
295
    window.location.reload();
296
}
297
function clickDisplay2DBuildings(cb) {
298
    createCookie('Map2DBuildings',cb.checked,2);
299
    window.location.reload();
300
}
301
302
function unitdistance(selectObj) {
303
    var idx = selectObj.selectedIndex;
304
    var unit = selectObj.options[idx].value;
305
    createCookie('unitdistance',unit,9999);
306
}
307
function unitspeed(selectObj) {
308
    var idx = selectObj.selectedIndex;
309
    var unit = selectObj.options[idx].value;
310
    createCookie('unitspeed',unit,9999);
311
    unitspeedvalue = unit;
0 ignored issues
show
Bug introduced by
The variable unitspeedvalue seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.unitspeedvalue.
Loading history...
312
}
313
function unitcoordinate(selectObj) {
314
    var idx = selectObj.selectedIndex;
315
    var unit = selectObj.options[idx].value;
316
    createCookie('unitcoordinate',unit,9999);
317
    unitcoordinate = unit;
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. unitcoordinate is already defined in line 313 as a function. While this will work, it can be very confusing.
Loading history...
318
}
319
function unitaltitude(selectObj) {
320
    var idx = selectObj.selectedIndex;
321
    var unit = selectObj.options[idx].value;
322
    createCookie('unitaltitude',unit,9999);
323
    unitaltitude = unit;
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. unitaltitude is already defined in line 319 as a function. While this will work, it can be very confusing.
Loading history...
324
}
325
326
function addarchive(begindate,enddate) {
327
    console.log('Add archive');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
328
    createCookie('archive',true,2);
329
    createCookie('archive_begin',begindate,2);
330
    createCookie('archive_end',enddate,2);
331
    createCookie('archive_speed',document.getElementById("archivespeed").value,2);
332
    window.location.reload();
333
}
334
function noarchive() {
335
    console.log('Exit archive!');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
336
    delCookie('archive');
337
    delCookie('archive_begin');
338
    delCookie('archive_end');
339
    delCookie('archive_speed');
340
    window.location.reload();
341
}
342
function msgbox(text,buttontext) {
343
	buttontext = buttontext || "OK";
344
	$("<div>" + text + "</div>").dialog({
345
	    dialogClass: "no-close",
346
	    buttons: [{
347
		text: buttontext,
348
		click: function() {
349
		    $( this ).dialog( "close" );
350
		    $(this).remove();
351
		}
352
	    }]
353
	});
354
}
355
function generateRandomPoint (latitude,longitude,height,diff,radius) {
356
357
	//console.log('height: '+height+' - diff: '+diff);
358
	radius = Math.random()*radius;
359
	latitude = latitude*(Math.PI/180.0);
360
	longitude = longitude*(Math.PI/180.0);
361
	
362
	const sinLat = 	Math.sin(latitude)
363
	const cosLat = 	Math.cos(latitude)
364
365
	/* go fixed distance in random direction*/
366
	const bearing = Math.random() * Math.PI*2
367
	const theta = radius/6371000
368
	const sinBearing = Math.sin(bearing)
369
	const cosBearing = Math.cos(bearing)
370
	const sinTheta = Math.sin(theta)
371
	const cosTheta = Math.cos(theta)
372
    
373
	latitude = Math.asin(sinLat*cosTheta+cosLat*sinTheta*cosBearing);
374
	longitude = longitude + Math.atan2( sinBearing*sinTheta*cosLat, cosTheta-sinLat*Math.sin(latitude ));
375
	/* normalize -PI -> +PI radians */
376
	longitude = ((longitude+(Math.PI*3))%(Math.PI*2))-Math.PI
377
	var h = height+(Math.random()*diff)
378
	//console.log('h: '+h);
379
	return {
380
	    latitude: latitude/(Math.PI/180.0),
381
	    longitude: longitude/(Math.PI/180.0),
382
	    height: h
383
	};
384
}
385
function getColor(colorStart,colorEnd,colorCount,step) {
386
	var alpha = (1.0/colorCount)*step;
387
	return {
388
	    r: colorStart[0]*alpha+(1-alpha)*colorEnd[0],
389
	    v: colorStart[1]*alpha+(1-alpha)*colorEnd[1],
390
	    b: colorStart[2]*alpha+(1-alpha)*colorEnd[2]
391
	};
392
}
393
function convertDMS(coord,latlong) {
394
	if (latlong == 'latitude') {
395
		var nsew = (coord >= 0) ? 'N' : 'S';
396
	} else if (latlong == 'longitude') {
397
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 395. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
398
	}
399
	var coord = Math.abs(coord);
400
	var deg = Math.floor(coord);
401
	var min = Math.floor((coord - deg) * 60);
402
	var sec = Math.round((coord - deg - min / 60) * 3600);
403
	var result = deg+"° "+min+"' "+sec+'" '+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 396 is false. Are you sure this can never be the case?
Loading history...
404
	return result;
405
}
406
function convertDM(coord,latlong) {
407
	if (latlong == 'latitude') {
408
		var nsew = (coord >= 0) ? 'N' : 'S';
409
	} else if (latlong == 'longitude') {
410
		var nsew = (coord >= 0) ? 'E' : 'W';
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable nsew already seems to be declared on line 408. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
411
	}
412
	var coord = Math.abs(coord);
413
	var deg = Math.floor(coord);
414
	var min = Math.round((coord - deg) * 60 *1000)/1000;
415
	var result = deg+"° "+min+"' "+nsew;
0 ignored issues
show
Bug introduced by
The variable nsew does not seem to be initialized in case latlong == "longitude" on line 409 is false. Are you sure this can never be the case?
Loading history...
416
	return result;
417
}
418